package com.yonyou.pmclouds.procedure.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.yonyou.pmclouds.ApplicationConsts;
import com.yonyou.pmclouds.basecom.exception.BusinessException;
import com.yonyou.pmclouds.offline.aspect.Offline;
import com.yonyou.pmclouds.offline.entity.OffLineDataKeyConst;
import com.yonyou.pmclouds.procedure.entity.ProcedureRefVO;
import com.yonyou.pmclouds.procedure.rmiitf.ProcedureQuery;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RequestMapping("/mobile/procedure")
@RestController
@Offline(dataFrom = OffLineDataKeyConst.DATA_FROM_PROCEDURE)
public class ProcedureMobController {
    @Reference(version = ApplicationConsts.APPLICATION_VERSION)
    private ProcedureQuery query;

    @GetMapping("/list")
    public ProcedureRefVO[] getTenantProcedures(@RequestParam("tenantId") String tenantId) throws BusinessException {
        // TODO 这个需要切换成上下文传递
        ProcedureRefVO[] allVOs = query.queryProcedureRefByTenant(tenantId);

        if (ArrayUtils.isEmpty(allVOs)) {
            return allVOs;
        }
        return buildResultVOs(allVOs);
    }

    @GetMapping("/listByPro")
    public ProcedureRefVO[] getProceByPro(@RequestParam("pkProject") String pkProject,@RequestParam("pkTenant") String tenantId) throws BusinessException {

        // TODO 这个需要切换成上下文传递
        ProcedureRefVO[] allVOs = query.queryRefByProject(pkProject,tenantId);

        if (ArrayUtils.isEmpty(allVOs))
            return new ProcedureRefVO[0];//

        return buildResultVOs(allVOs);
    }

    /**
     * 构建树结构
     *
     * @param allVOs
     * @return
     */
    private ProcedureRefVO[] buildResultVOs(ProcedureRefVO[] allVOs) {
        // 构造树
        Map<String, List<ProcedureRefVO>> procedureChildMap = new HashMap<>();
        for (ProcedureRefVO vo : allVOs) {
            String parent = vo.getPkParent();
            if (StringUtils.isEmpty(parent)) {
                parent = StringUtils.EMPTY;
            }
            List<ProcedureRefVO> children = procedureChildMap.get(parent);
            if (children == null) {
                children = new ArrayList<>();
                procedureChildMap.put(parent, children);
            }
            children.add(vo);
        }
        for (ProcedureRefVO vo : allVOs) {
            List<ProcedureRefVO> children = procedureChildMap.get(vo.getPkProcedure());
            if (children != null) {
                vo.setChildProcedure(children.toArray(new ProcedureRefVO[children.size()]));
            }
        }
        List<ProcedureRefVO> result = procedureChildMap.get(StringUtils.EMPTY);
        if (result == null) {
            return new ProcedureRefVO[0];
        } else {
            return result.toArray(new ProcedureRefVO[result.size()]);
        }
    }


}
